home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-03-05 | 6.1 KB | 235 lines |
- /*
- * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
- * modify and redistribute this software in source and binary code form,
- * provided that i) this copyright notice and license appear on all copies of
- * the software; and ii) Licensee does not utilize the software in a manner
- * which is disparaging to Sun.
- *
- * This software is provided "AS IS," without a warranty of any kind. ALL
- * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
- * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
- * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
- * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
- * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
- * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
- * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
- * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
- * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGES.
- *
- * This software is not designed or intended for use in on-line control of
- * aircraft, air traffic, aircraft navigation or aircraft communications; or in
- * the design, construction, operation or maintenance of any nuclear
- * facility. Licensee represents and warrants that it will not use or
- * redistribute the Software for such purposes.
- */
-
- /**
- * This class is the Portfolio Server implementation
- * @author Rahul
- */
-
- import java.awt.*;
- import java.util.*;
- import java.net.*;
- import java.io.*;
-
- import PortfolioManager.*;
-
- import com.sun.CORBA.iiop.*;
- import org.omg.CORBA.*;
- import org.omg.CosNaming.*;
- import org.omg.CosNaming.NamingContextPackage.*;
-
-
- /** Servant class implementation for Portfolio
- **/
- class
- PortfolioServantImpl extends PortfolioManager._PortfolioImplBase {
-
- /** Map for Stocks in Portfolio
- **/
- private Hashtable stocksMap_;
-
- /** Constructor
- **/
- public
- PortfolioServantImpl() {
- stocksMap_ = new Hashtable();
- }
-
- public
- PortfolioManager.Stock addStock(String ticker, short number, double price)
- {
-
- StockServantImpl _servant = new StockServantImpl(ticker, number, price);
- if (stocksMap_.containsKey(ticker) == true) {
- System.out.println("Portfolio: Already contains Stock with ticker " +
- ticker);
- return null;
- }
- else {
- stocksMap_.put(ticker, _servant);
- PortfolioServer.theOrb_.connect(_servant);
- return _servant;
- }
- }
-
- public
- void deleteStock(String ticker) {
- stocksMap_.remove(ticker);
- }
-
- public
- PortfolioManager.Stock getStock(String ticker) {
- StockServantImpl _servant = (StockServantImpl)stocksMap_.get(ticker);
- PortfolioServer.theOrb_.connect(_servant);
- return _servant;
- }
-
- public
- void buy(String ticker, short number) {
- if (stocksMap_.containsKey(ticker) == true) {
- StockServantImpl _servant = (StockServantImpl)stocksMap_.get(ticker);
- _servant.number_ += number;
- }
- else {
- System.out.println("PortfolioServer: Invalid Transaction");
- }
- }
-
- public
- void sell(String ticker, short number) {
- if (stocksMap_.containsKey(ticker) == true) {
- StockServantImpl _servant = (StockServantImpl)stocksMap_.get(ticker);
- if (_servant.number_ >= number) {
- _servant.number_ -= number;
- }
- }
- else {
- System.out.println("PortfolioServer: Invalid Transaction");
- }
- }
-
- public
- double value() {
-
- double totalValue = 0;
-
- Enumeration stocks = stocksMap_.elements();
- while (stocks.hasMoreElements() == true) {
- StockServantImpl stock = (StockServantImpl)stocks.nextElement();
- totalValue += stock.number_ * stock.price_;
- }
-
- return totalValue;
- }
-
- public
- String report() {
-
- StringBuffer _report = new StringBuffer();
- Enumeration stocks = (Enumeration)stocksMap_.elements();
- while (stocks.hasMoreElements() == true) {
- StockServantImpl stock = (StockServantImpl)stocks.nextElement();
- _report.append(stock.toString()).append("\n");
- }
- return _report.toString();
- }
-
- }
-
- /** Servant Class implementation for Stock
- **/
- class
- StockServantImpl extends PortfolioManager._StockImplBase {
- /** Stock Data
- **/
- public String ticker_;
- public short number_;
- public double price_;
-
- /** Constructor
- **/
- public
- StockServantImpl(String ticker, short number, double price) {
- ticker_ = ticker;
- number_ = number;
- price_ = price;
- }
-
-
- /** print
- **/
- public
- String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append(this.ticker_).append(": ").append("Number: ").
- append(this.number_).append(" Price: ").append(this.price_);
- return sb.toString();
- }
-
- public
- String ticker() {
- return this.ticker_;
- }
-
- public
- short number() {
- return this.number_;
- }
-
- public
- double price() {
- return this.price_;
- }
-
- public
- void price(double arg) {
- price_ = arg;
- }
-
- }
-
-
- public class PortfolioServer {
-
- public static org.omg.CORBA.ORB theOrb_;
-
- /** Main
- **/
- public static void main(String[] args) {
- try {
- // create and initialize ORB
- theOrb_ = org.omg.CORBA.ORB.init(args, null);
-
- // create Servant and register it with ORB
- PortfolioServantImpl servant = new PortfolioServantImpl();
- theOrb_.connect(servant);
-
- // get the root naming context
- org.omg.CORBA.Object objRef =
- theOrb_.resolve_initial_references("NameService");
- NamingContext ncRef = NamingContextHelper.narrow(objRef);
-
- // bind the Object Reference in Naming
- NameComponent nc = new NameComponent("Portfolio", "");
- NameComponent path[] = {nc};
- ncRef.rebind(path, servant);
-
- // wait for invocations from clients
- java.lang.Object sync = new java.lang.Object();
- synchronized (sync) {
- sync.wait();
- }
- }
- catch (Exception ex) {
- ex.printStackTrace();
- System.err.println("Portfolio Server: Exception-> " + ex);
- }
- }
-
- }
-